<script setup lang="ts">
import { getCurrentInstance, redirectTo, showLoading, showToast } from '@tarojs/taro';
import { APPROVAL_CHANGE_ALLOCATION } from '/@/api/modules/archive/archive-change-allocation';
import { APPROVAL_CHANGE_ALTER } from '/@/api/modules/archive/archive-change-alter';
import { APPROVAL_CHANGE_BORROW } from '/@/api/modules/archive/archive-change-borrow';
import { APPROVAL_CHANGE_HANDLE } from '/@/api/modules/archive/archive-change-handle';
import { APPROVAL_CHANGE_RECEIVE } from '/@/api/modules/archive/archive-change-receive';
import { state } from './state';
/**
 * @description 当前路由信息
 */
const params = getCurrentInstance().router?.params as {
  id: string;
  /**
   * 变动类型：1借用归还 2领用归还 3档案调拨 4档案处置 5档案变更
   */
  changeType?: string;
};

/**
 *申请同意
 */
function agree() {
  commonHandle(1);
}

function approveBorrow(approvalResult: number) {
  APPROVAL_CHANGE_BORROW({
    changeId: params.id,
    approvalResult,
    approvalRemark: state.approvalRemark,
  }).then((res) => {
    if (res.comResp?.code === 100) {
      redirectTo({
        url: `/pages/archive-manage/approve-list/index`,
      });
    }
  });
}

function approveReceive(approvalResult: number) {
  APPROVAL_CHANGE_RECEIVE({
    changeId: params.id,
    approvalResult,
    approvalRemark: state.approvalRemark,
  }).then((res) => {
    if (res.comResp?.code === 100) {
      redirectTo({
        url: `/pages/archive-manage/approve-list/index`,
      });
    }
  });
}

function approveAllocation(approvalResult: number) {
  APPROVAL_CHANGE_ALLOCATION({
    changeId: params.id,
    approvalResult,
    approvalRemark: state.approvalRemark,
  }).then((res) => {
    if (res.comResp?.code === 100) {
      redirectTo({
        url: `/pages/archive-manage/approve-list/index`,
      });
    }
  });
}

function approveHandle(approvalResult: number) {
  APPROVAL_CHANGE_HANDLE({
    changeId: params.id,
    approvalResult,
    approvalRemark: state.approvalRemark,
  }).then((res) => {
    if (res.comResp?.code === 100) {
      redirectTo({
        url: `/pages/archive-manage/approve-list/index`,
      });
    }
  });
}

function approveAlter(approvalResult: number) {
  APPROVAL_CHANGE_ALTER({
    changeId: params.id,
    approvalResult,
    approvalRemark: state.approvalRemark,
  }).then((res) => {
    if (res.comResp?.code === 100) {
      redirectTo({
        url: `/pages/archive-manage/approve-list/index`,
      });
    }
  });
}

/**
 * 申请拒绝
 */
function refuse() {
  commonHandle(2);
}

function commonHandle(approvalResult: number) {
  if (!state.approvalRemark) {
    showToast({
      title: '请选择处理人',
      icon: 'error',
    });
    return;
  }
  showLoading();
  switch (params.changeType) {
    case '1':
      approveBorrow(approvalResult);
      break;
    case '2':
      approveReceive(approvalResult);
      break;
    case '3':
      approveAllocation(approvalResult);
      break;
    case '4':
      approveHandle(approvalResult);
      break;
    case '5':
      approveAlter(approvalResult);
      break;
    default:
      break;
  }
}
</script>

<template>
  <view class="asset-approve-edit app-page--1">
    <app-navbar title="审批" />
    <scroll-view class="app-page__wrapper" :scroll-y="true" :lower-threshold="100">
      <view class="asset-approve-edit__wrapper">
        <nut-form>
          <nut-form-item>
            <nut-textarea
              v-model="state.approvalRemark"
              placeholder="填写审批意见"
              rows="8"
              limit-show
              text-align="right"
              max-length="200"
            />
          </nut-form-item>
        </nut-form>
      </view>
    </scroll-view>
    <view class="asset-approve-edit__footer">
      <nut-button class="asset-approve-edit__footer-btn" type="info" @click="agree()"
        >同意</nut-button
      >
      <nut-button class="asset-approve-edit__footer-btn" type="danger" @click="refuse()"
        >拒绝</nut-button
      >
    </view>
  </view>
</template>

<style lang="scss">
@import './index.scss';
</style>
